package love.simbot.example.utils.chatRobot;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import love.simbot.example.entity.chatRobot.MoliChatContent;
import love.simbot.example.entity.chatRobot.MoliChatRobot;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

/**
 *  茉莉机器人
 */
public class RobotUtils {

    private static final String MOLI_URL = "https://api.mlyai.com/reply";

    private static Properties config = new Properties();
    static {
        FileInputStream input = null;
        try {
            input = new FileInputStream("src\\main\\resources\\config.properties");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        try {
            config.load(input);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static final String API_KEY = config.getProperty("moliKey");
    private static final String API_SECRET = config.getProperty("moliSecret");

    public static MoliChatRobot getChatContent(String content,
                                               String from,
                                               String fromName,
                                               String to,
                                               String toName,
                                               Integer type) {
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(MOLI_URL);
        httpPost.setHeader("Api-Key",API_KEY);
        httpPost.setHeader("Api-Secret",API_SECRET);
        httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");

        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
        MoliChatContent moliContent = new MoliChatContent();
        moliContent.setContent(content);
        moliContent.setFrom(from);
        moliContent.setFromName(fromName);
        moliContent.setTo(to);
        moliContent.setToName(toName);
        moliContent.setType(type);

        //jackson把bean变成json
        String sendMsgParamsVoStr = null;
        try {
            sendMsgParamsVoStr = mapper.writeValueAsString(moliContent);
            StringEntity stringEntity = new StringEntity(sendMsgParamsVoStr,"UTF-8");
            httpPost.setEntity(stringEntity);
            CloseableHttpResponse response = client.execute(httpPost);
            if(response.getStatusLine().getStatusCode() == 200 ) {
                HttpEntity entity = response.getEntity();
                String result = EntityUtils.toString(entity);
                MoliChatRobot chatRobot = mapper.readValue(result, MoliChatRobot.class);
                return chatRobot;
            }
            return null;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

}
